//
//  ReminderViewController.m
//  FootPrint
//
//  Created by alex tao on 4/28/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "ReminderViewController.h"
#import "GReminder.h"

@interface ReminderViewController ()

@end

@implementation ReminderViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        [self loadIndex];
        
        _selIdx = -1;
        
        _picker = [[CountDownPicker alloc] initWithFrame:CGRectMake(0.0, 480.0, 320.0, 216.0)];
        _picker.delegate = self;
    }
    return self;
}

- (NSString *)indexFilePath
{
    if (nil == _indexFile) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *baseDir = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        
        _indexFile = [[baseDir stringByAppendingPathComponent:@"reminder.idx"] copy];
    }
    return _indexFile;
}

- (void) loadIndex
{
    _presetReminder = [[NSMutableArray alloc] initWithContentsOfFile:[self indexFilePath]];
    if (nil == _presetReminder) {
        _presetReminder = [[NSMutableArray alloc] initWithObjects:@"300", @"900", @"1800", @"3600", @"7200", @"21600", @"43200", @"86400", nil];
        // 默认5分钟，15分钟，30分钟，1小时，2小时，6小时，12小时，1天
    }
}

- (void) saveIndex
{
    [_presetReminder writeToFile:[self indexFilePath] atomically:YES];
}

- (void) updateDateInfo
{
    [_presetReminder enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIButton * btn = (UIButton*)[_baseView viewWithTag:idx+1];
        btn.titleLabel.numberOfLines = 3;
        NSInteger secondBefore = [obj integerValue];
        [btn setTitle:[GToolUtil stringBySeconds:secondBefore] forState:UIControlStateNormal];
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self updateDateInfo];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_picker release];
    [_presetReminder release];
    [_indexFile release];
    
    [_baseView release];
    [_reminderLabel release];
    [super dealloc];
}


- (void)viewDidUnload {
    [self setBaseView:nil];
    [self setReminderLabel:nil];
    [super viewDidUnload];
}

- (IBAction)exit:(id)sender {
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}

- (IBAction)pickDate:(id)sender
{
    _selIdx = ((UIButton*)sender).tag - 1;

    if (_selIdx >= 0 && _selIdx < _presetReminder.count) {
        NSInteger pickSecond = [[_presetReminder objectAtIndex:_selIdx] integerValue];
        if (_reminderMode == eReminderModifyMode) {
            [_picker showWithTime:pickSecond/60];
        } else {
            [_reminderDelegate didSelectSecondBefore:pickSecond];
            [self exit:nil];
        }
    } else if (_selIdx == _presetReminder.count) {
        if (_reminderMode == eReminderPickMode) {
            [_picker showWithTime:0];
        }
    }
}

- (void) updateTitleWithEvent:(NSString*)eventId andAimedDate:(NSDate *)date
{
    UILocalNotification * notify = [GReminder notificationForKey:eventId];
    if (notify && date) {
        NSInteger seconds = [date timeIntervalSinceDate:notify.fireDate];
        _reminderLabel.text = [NSString stringWithFormat:@"提前%@提醒", [GToolUtil stringBySeconds:seconds]];
    } else {
        _reminderLabel.text = @"设置提醒";
    }
}


#pragma mark - CountDownPicker

- (BOOL)timeSelected:(NSInteger)minutes
{
    if (_reminderMode == eReminderModifyMode && _selIdx < _presetReminder.count) {
        [_presetReminder replaceObjectAtIndex:_selIdx withObject:I2N(minutes*60)];
        [self saveIndex];
        [self updateDateInfo];
    } else if (_reminderMode == eReminderPickMode && _selIdx == _presetReminder.count) {
        [_reminderDelegate didSelectSecondBefore:minutes*60];
        [self exit:nil];
    }
    return YES;
}

@end
